package p.s;

import java.io.File;
import java.io.IOException;

/**
 * Convenient pattern for log indeces.
 */
public class Index
    extends java.io.FileWriter
{
    public final static String CRLF = "\r\n";

    private final static File Ctor(File file)
        throws IOException
    {
        if (file.isDirectory())
            return new File(file,"index.txt");
        else {
            File dir = file.getParentFile();
            if (!dir.exists())
                if (!dir.mkdirs())
                    throw new IOException("Unable to create parent directory '"+dir.getPath()+"'.");
            return file;
        }
    }


    public Index(File file)
        throws IOException
    {
        super(Ctor(file),true);
    }


    public void println(String string)
        throws IOException
    {
        if (null != string)
            this.append(string);
        this.append(CRLF);
        this.flush();
    }
    public void log(String string)
        throws IOException
    {
        if (null != string)
            this.append(string);
        this.append(CRLF);
        this.flush();
    }
}
